/** 
 * 全局变量定义
 */
var INDEX_WEBAPP = "";

// 图片服务器地址
var INDEX_IMGAPP = "http://114.215.136.142:8999/"

var SUCCESS = "1";

var ADD = "add";

var UPDATE = "update";

// 异步
var ASYNC = true;

// 同步
var SYNC = false;

// 需要加载动画
var NEED_LOAD = true;

// 不需要加载动画
var NO_NEED_LOAD = false;

var INDEX_COMMODITYSTATUS = {
		notFinish   : 0,
		auditFailed : 1,
		auditSuccess: 2,
		notAudit    : 3,
		under       : 4
};

//分页对象
var index_pageObj = 
{
	currentPage : 0,
	pageSize : 0,
	isEnd : false,
	init : function ()
	{
		this.currentPage = 1;
		this.pageSize = 10;
		this.isEnd = false;
		$("#crtPage").html(1);
	},
	isEndMethod : function (data)
	{
		// 无数据时，当前页为最后一页
		if (!data)
		{
			this.isEnd = true;
		}
		else
		{
			// 数据小于每页显示数时，当前页为最后一页
			if (data.length < this.pageSize)
			{
				this.isEnd = true;
			}
			else
			{
				this.isEnd = false;
			}
		}
	},
	toEndPage : function ()
	{
		if (index_pageObj.currentPage == 1)
		{
			return false;
		}
		index_pageObj.currentPage = index_pageObj.currentPage -1;
		$("#crtPage").html(index_pageObj.currentPage);
		index_show("已经是最后一页。");
	}
};

//消息
function index_show (msg, delay) {
	msg = (msg || "操作失败，请联系管理员。");
	var wrap = $('<div class="notify-wrapper">').appendTo('body');
    var dom = $('<div class="notify-item" style="display:none;">').html(msg).appendTo(wrap).fadeIn(800);
    setTimeout(function () {
      dom.animate({ left: -30, opacity: 0 }).slideUp(500, function () {
        $(this).remove();
      });
    }, delay || 2500);
}

//初始化分页事件
function index_initPageEvent(pageMethod)
{
	$("#page_prev").click(function(){
		if (index_pageObj.currentPage == 1)
		{
			index_show("当前已经是第一页。");
			return false;
		}
		index_pageObj.currentPage = index_pageObj.currentPage - 1;
		$("#crtPage").html(index_pageObj.currentPage);
		eval(pageMethod);
	});	
	
	$("#page_next").click(function(){
		if (index_pageObj.isEnd)
		{
			index_show("已经是最后一页。");
			return false;
		}
		index_pageObj.currentPage = index_pageObj.currentPage + 1;
		$("#crtPage").html(index_pageObj.currentPage);
		eval(pageMethod);
	});
}

//加载开始
function loadingStart(divId){
	var id = "loading_div";
	if (divId)
	{
		id = divId;
	}
	$("#"+id ,window.parent.document).css("z-index" ,1);
	$('#'+id ,window.parent.document).waitMe({
		effect: 'bounce',
		text: '加载中,请稍候...',
		bg: 'rgba(255,255,255,0.7)',
		color:'#000',
		sizeW:'',
		sizeH:''
	});
}

// 加载结束
function loadingEnd(divId)
{
	var id = "loading_div";
	if (divId)
	{
		id = divId;
	}
	$("#"+id ,window.parent.document).css("z-index" , -1);
	$('#'+id ,window.parent.document).waitMe('hide');
}

function um_ajax_get(url ,paramObj ,successCallBack , failCallBack ,isAsync ,isLoad)
{
	um_ajax("GET" ,url ,paramObj ,successCallBack , failCallBack ,isAsync ,isLoad);
}

function um_ajax_post(url ,paramObj ,successCallBack , failCallBack ,isAsync ,isLoad)
{
	um_ajax("POST" ,url ,paramObj ,successCallBack , failCallBack ,isAsync ,isLoad);
}

function um_ajax(type ,url ,paramObj ,successCallBack , failCallBack ,isAsync ,isLoad)
{
	if (isAsync == undefined || isAsync == null || isAsync == true)
	{
		isAsync = true;
	}
	else
	{
		isAsync = false;
	}
	
	if (isLoad == undefined || isLoad == null || isLoad == true)
	{
		isLoad = true;
	}
	else
	{
		isLoad = false;
	}
	$.ajax({
		async: isAsync,
		type: type,
		url: url,
		dataType: "json",
		data:{data:JsonTools.encode(paramObj)},
		success :function(data)
		{
			if (data.result_code == 1)
			{
				successCallBack(data.result_map);
			}
			else
			{
				$("body").umDialog("alert" ,{"content":data.result_message,left:"40%"});
				if (failCallBack)
				{
					failCallBack();
				}
			}
			if (isLoad)
			{
				loadingEnd();
			}
		},
		error :function(){
			$("body").umDialog("alert" ,{"content":"请求地址：" + url + JsonTools.encode(paramObj) + "发生错误",left:"40%"});
		}
	});
}

function getQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

function beautyNull(str)
{
	if (str == null || str == undefined)
	{
		return "";
	}
	return str;
}

function setChkValue(chk,val)
{
	if (val == 1)
	{
		chk.attr("class" ,"icon-check");
	}
	else
	{
		chk.attr("class" ,"icon-check-empty");
	}
}

function getChkValue(chk)
{
	if (chk.attr("class") == "icon-check-empty")
	{
		return 0;
	}
	return 1;
}